#pragma once

#include "tcpServer.hpp"
/*#include "protocol.hpp"// 解耦*/
#include "threadPool.hpp"
#include "task.hpp"

#include <pthread.h>
#include <signal.h>
class HttpServer
{
private:
    TcpServer *_tcp_ptr; // 也可以用单例将他们变为组件，就不要在这里作为成员变量了
    ThreadPool *_pool_ptr;
    uint16_t _port;
    bool _stop;

public:
    HttpServer(uint16_t port) : _port(port), _stop(false)
    {
        signal(SIGPIPE, SIG_IGN); // 防止再写出错时，结束我们的服务器运行
        init();
    }
    void init()
    {
        _tcp_ptr = TcpServer::getTcpServer(_port);
        _pool_ptr = ThreadPool::getThreadPool();
    }
    void loop()
    {
        std::string remoteIp;
        uint16_t remotePort;
        while (!_stop)
        {
            int sock = _tcp_ptr->getListenSock()->Accept(remoteIp, remotePort);
            if (sock == -1)
                continue;
            // pthread_t tid;
            // int *sockPtr = new int(sock);
            // pthread_create(&tid, nullptr, Entrance::HandleRequest, sockPtr);
            // pthread_detach(tid);
            Task task(sock);
            _pool_ptr->pushTask(task);
        }
    }
};